package cn.bingo.yunsj.config;

import cn.bingo.yunsj.config.model.MyAuthenticationFailerHandler;
import cn.bingo.yunsj.config.model.MyAuthenticationSuccessHandler;
import org.apache.catalina.filters.CorsFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.servlet.config.annotation.CorsRegistry;

@EnableWebSecurity
@CrossOrigin
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;

    @Autowired
    private MyAuthenticationFailerHandler myAuthenticationFailerHandler;

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(this.userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 跨域的问题
        http.headers().frameOptions().disable();

        http.cors().and()
                .antMatcher("/")
                .authorizeRequests()
                .antMatchers("http://10.201.61.194:8099/#/login**","/login","/static/**","/registry/**","/swagger-ui.html","/webjars/**").permitAll()
                .anyRequest().authenticated()
                .and()
                //注销
                .logout()
                //触发注销操作
                .logoutUrl("/logout")
                //注销成功后的跳转
                .logoutSuccessUrl("/login")
                .and()
                .formLogin()
                //指定登录页的路径
                //http://localhost:8080/#/rule
                .loginPage("http://10.201.61.194:8099/#/login")
                //指定自定义form表单请求的路径
                .loginProcessingUrl("/user_login")
                .successHandler(myAuthenticationSuccessHandler)
                .failureHandler(myAuthenticationFailerHandler)
                //必须允许所有用户访问我们的登录页（例如未验证的用户，否则验证流程就会进入死循环）
                //这个formLogin().permitAll()方法允许所有用户基于表单登录访问/login这个page。
                .permitAll();
        //默认都会产生一个hiden标签 里面有安全相关的验证 防止请求伪造 这边我们暂时不需要 可禁用掉
        http.csrf().disable();
    }
}
